<?php
namespace User\Controller;
use Think\Controller;
class FindpswController extends Controller {
	function __construct(){
		parent::__construct();			
			$not = array('index','verify','oper');
			if(!in_array(ACTION_NAME,$not)){
				
				if(empty($_SESSION['findId'])){
					$this->redirect('index', array() , 0, '');
				}
			}			
	}  
	//找回密码首页
    public function index(){
		$this->display();
    }
	
	//验证码输出
	public function verify(){
		
		$Verify = new \Think\Verify();
		$Verify->fontSize = 14;
		$Verify->length = 4;
		$Verify->imageH = 35;
		$Verify->fontttf = '7.ttf'; 
		$Verify->useCurve =false;
		$Verify->useImgBg =true;
		$Verify->imageW = 100;
		$Verify->useNoise = false;
		
		$Verify->entry();	
		
	}
	/*
	*1.用户ID验证
	*2.密保问题验证
	*/
	public function oper(){
		
		if($_POST){
			$from = I('post.from');
			if($from==1){
				$code = I('post.code');
				$userId = I('post.userid');
				if(!check_verify($code)){
					echo 1;
				}else if($userId){
					$Model = M('tbuserinformation');
					$checkId = $Model->where("nuserid=$userId")->count();
					if(!$checkId){
						echo 2;
					}else{
						$checkQues = $Model->field('cpwdq1,cpwdq2')->where("nuserid=$userId")->find();
						if(!empty($checkQues['cpwdq1']) && !empty($checkQues['cpwdq2'])){
							session('findId',$userId);
							echo 3;
						}else{							
							echo 4;
						}
					}
				
				}
				
			}else if($from==2){
				$userId = I('session.findId');
				if($userId){
					$data = M('tbuserinformation')->field('cpwda1,cpwda2')->where("nuserid=$userId")->find();
					$ans1 = $cpwdq['cpwda1'];
					$ans2= $cpwdq['cpwda2'];
					$da1 = I('post.ans1','');
					$da2 = I('post.ans2','');
					$cpwda1 =CreatePassword($da1);//密保问题1答案
					$cpwda2 =CreatePassword($da2);//密保问题2答案
					if($cpwda1!=$data['cpwda1']){
						echo 1; //问题1答案错误
					}else if($cpwda2!=$data['cpwda2']){
						echo 2;//问题2答案错误
					}else if($cpwda1==$data['cpwda1'] && $cpwda2==$data['cpwda2']){
						echo 3;
						session('question','ok');
					   }										
				}
				
			}else if($from==3){
				$userId = I('session.findId');
				$psw= I('post.psw'); 
				$npsw['cpassword'] = CreatePassword($psw);
				if($userId){
					$id = M('tbuserinformation')->where("nuserid=$userId")->save($npsw);
					if($id){
						echo 1;
					}
				}
			
				
			}
			
			
		}
		
	}
  
	public function question(){
		$userId = I('session.findId');
		
		$checkQues = M('tbuserinformation')->field('cpwdq1,cpwdq2')->where("nuserid=$userId")->find();
		$this->assign('dq1',$checkQues['cpwdq1']);
		$this->assign('dq2',$checkQues['cpwdq2']);
		$this->display();
	}
	
	public function resetpsw(){
		if($_SESSION['question']!='ok'){
			$this->redirect('index', array() , 0, '');
		}else{
			$this->display();
		}
		
	}
}